package com.tools.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tools.dao.ThemeDao;
import com.tools.dao.ThemeDetailDao;
import com.tools.entity.Theme;
import com.tools.entity.ThemeDetail;
import com.tools.service.ThemeService;

@Service
@Transactional
public class ThemeServiceImpl implements ThemeService {

	@Autowired 
	private ThemeDao themeDao;
	@Autowired
	private ThemeDetailDao themeDetailDao;
	public List<Theme> getAll() {
		// TODO Auto-generated method stub
		return themeDao.getAll();
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public boolean insert(Theme theme) {
		// TODO Auto-generated method stub
		boolean boo=themeDao.insert(theme)>0;
		for (ThemeDetail themeDetail : theme.getThemeDetails()) {
			themeDetail.setTid(theme.getId());
			boo=boo&&themeDetailDao.insert(themeDetail)>0;
		}
		return boo;
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		boolean boo=themeDao.delete(id)>0;
		themeDetailDao.delete(id);
		return boo;
	}
	public Theme fetchById(int id) {
		// TODO Auto-generated method stub
		return themeDao.fetchById(id);
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public boolean update(Theme theme) {
		// TODO Auto-generated method stub
		boolean boo=themeDao.update(theme)>0;
		themeDetailDao.delete(theme.getId());
		for (ThemeDetail themeDetail : theme.getThemeDetails()) {
			themeDetail.setTid(theme.getId());
			boo=boo&&themeDetailDao.insert(themeDetail)>0;
		}
		return boo;
	}

	
}
